Repository navigation
fix(api): fix legacy model_type query compatibility for provider reads - #36427
QuantumGhost wants to merge 7 commits into
Conversation
Assisted-by: Codex:GPT-5.4
Pyrefly Diffbase → PR--- /tmp/pyrefly_base.txt 2026-05-21 06:31:45.691800849 +0000
+++ /tmp/pyrefly_pr.txt 2026-05-21 06:31:31.491656047 +0000
@@ -41,7 +41,7 @@
ERROR No matching overload found for function `core.model_manager.ModelInstance.invoke_llm` called with arguments: (prompt_messages=list[SystemPromptMessage | UserPromptMessage], model_parameters=dict[str, float], stream=Literal[False]) [no-matching-overload]
--> core/llm_generator/llm_generator.py:653:60
ERROR Class member `OpsTraceProviderConfigMap.__getitem__` overrides parent class `UserDict` in an inconsistent manner [bad-override-param-name]
- --> core/ops/ops_trace_manager.py:218:9
+ --> core/ops/ops_trace_manager.py:226:9
ERROR `list[dict[str, str | dict[str, str]]] | list[Unknown]` is not assignable to TypedDict key `tool_calls` with type `list[dict[str, str]] | str` [bad-typed-dict-key]
--> core/prompt/utils/prompt_message_util.py:83:44
ERROR Argument `Unknown | None` is not assignable to parameter `result_object` with type `dict[str, Any]` in function `WaterCrawlProvider._structure_data` [bad-argument-type]
@@ -3991,21 +3991,23 @@
ERROR Argument `SimpleNamespace` is not assignable to parameter `db_provider` with type `MCPToolProvider` in function `core.entities.mcp_provider.MCPProviderEntity.from_db_model` [bad-argument-type]
--> tests/unit_tests/core/entities/test_entities_mcp_provider.py:59:46
ERROR `None` is not subscriptable [unsupported-operation]
- --> tests/unit_tests/core/entities/test_entities_provider_configuration.py:666:12
+ --> tests/unit_tests/core/entities/test_entities_provider_configuration.py:667:12
ERROR Argument `SimpleNamespace` is not assignable to parameter `credential_record` with type `ProviderCredential | ProviderModelCredential` in function `core.entities.provider_configuration.ProviderConfiguration._update_load_balancing_configs_with_credential` [bad-argument-type]
- --> tests/unit_tests/core/entities/test_entities_provider_configuration.py:894:31
+ --> tests/unit_tests/core/entities/test_entities_provider_configuration.py:895:31
ERROR Argument `SimpleNamespace` is not assignable to parameter `credential_record` with type `ProviderCredential | ProviderModelCredential` in function `core.entities.provider_configuration.ProviderConfiguration._update_load_balancing_configs_with_credential` [bad-argument-type]
- --> tests/unit_tests/core/entities/test_entities_provider_configuration.py:912:27
-ERROR `None` is not subscriptable [unsupported-operation]
- --> tests/unit_tests/core/entities/test_entities_provider_configuration.py:1027:12
+ --> tests/unit_tests/core/entities/test_entities_provider_configuration.py:913:27
ERROR `None` is not subscriptable [unsupported-operation]
--> tests/unit_tests/core/entities/test_entities_provider_configuration.py:1028:12
ERROR `None` is not subscriptable [unsupported-operation]
- --> tests/unit_tests/core/entities/test_entities_provider_configuration.py:1044:12
+ --> tests/unit_tests/core/entities/test_entities_provider_configuration.py:1029:12
+ERROR `None` is not subscriptable [unsupported-operation]
+ --> tests/unit_tests/core/entities/test_entities_provider_configuration.py:1045:12
+ERROR `None` is not subscriptable [unsupported-operation]
+ --> tests/unit_tests/core/entities/test_entities_provider_configuration.py:1073:12
ERROR `None` is not subscriptable [unsupported-operation]
- --> tests/unit_tests/core/entities/test_entities_provider_configuration.py:1533:12
+ --> tests/unit_tests/core/entities/test_entities_provider_configuration.py:1565:12
ERROR `None` is not subscriptable [unsupported-operation]
- --> tests/unit_tests/core/entities/test_entities_provider_configuration.py:1725:12
+ --> tests/unit_tests/core/entities/test_entities_provider_configuration.py:1757:12
ERROR Object of class `ModuleType` has no attribute `MockExtension` [missing-attribute]
--> tests/unit_tests/core/extension/test_extensible.py:62:9
ERROR Cannot index into `OrderedDict[str, ModuleExtension]` [bad-index]
@@ -6171,53 +6173,55 @@
ERROR Argument `None` is not assignable to parameter `name` with type `str` in function `services.metadata_service.MetadataService.update_metadata_name` [bad-argument-type]
--> tests/unit_tests/services/test_metadata_nullable_bug.py:51:85
ERROR Object of class `ModelLoadBalancingService` has no attribute `provider_manager` [missing-attribute]
- --> tests/unit_tests/services/test_model_load_balancing_service.py:76:5
-ERROR Object of class `ModelLoadBalancingService` has no attribute `model_assembly` [missing-attribute]
--> tests/unit_tests/services/test_model_load_balancing_service.py:77:5
+ERROR Object of class `ModelLoadBalancingService` has no attribute `model_assembly` [missing-attribute]
+ --> tests/unit_tests/services/test_model_load_balancing_service.py:78:5
+ERROR Object of class `ModelLoadBalancingService` has no attribute `provider_manager` [missing-attribute]
+ --> tests/unit_tests/services/test_model_load_balancing_service.py:105:5
ERROR Object of class `ModelLoadBalancingService` has no attribute `provider_manager` [missing-attribute]
- --> tests/unit_tests/services/test_model_load_balancing_service.py:104:5
+ --> tests/unit_tests/services/test_model_load_balancing_service.py:125:5
ERROR Object of class `ModelLoadBalancingService` has no attribute `provider_manager` [missing-attribute]
- --> tests/unit_tests/services/test_model_load_balancing_service.py:124:5
+ --> tests/unit_tests/services/test_model_load_balancing_service.py:136:5
ERROR Object of class `ModelLoadBalancingService` has no attribute `provider_manager` [missing-attribute]
- --> tests/unit_tests/services/test_model_load_balancing_service.py:135:5
+ --> tests/unit_tests/services/test_model_load_balancing_service.py:154:5
ERROR Object of class `ModelLoadBalancingService` has no attribute `provider_manager` [missing-attribute]
- --> tests/unit_tests/services/test_model_load_balancing_service.py:153:5
+ --> tests/unit_tests/services/test_model_load_balancing_service.py:208:5
ERROR Object of class `ModelLoadBalancingService` has no attribute `provider_manager` [missing-attribute]
- --> tests/unit_tests/services/test_model_load_balancing_service.py:204:5
+ --> tests/unit_tests/services/test_model_load_balancing_service.py:266:5
ERROR Object of class `ModelLoadBalancingService` has no attribute `provider_manager` [missing-attribute]
- --> tests/unit_tests/services/test_model_load_balancing_service.py:258:5
+ --> tests/unit_tests/services/test_model_load_balancing_service.py:328:5
ERROR Object of class `ModelLoadBalancingService` has no attribute `provider_manager` [missing-attribute]
- --> tests/unit_tests/services/test_model_load_balancing_service.py:271:5
+ --> tests/unit_tests/services/test_model_load_balancing_service.py:341:5
ERROR Object of class `ModelLoadBalancingService` has no attribute `provider_manager` [missing-attribute]
- --> tests/unit_tests/services/test_model_load_balancing_service.py:290:5
+ --> tests/unit_tests/services/test_model_load_balancing_service.py:360:5
ERROR Object of class `ModelLoadBalancingService` has no attribute `provider_manager` [missing-attribute]
- --> tests/unit_tests/services/test_model_load_balancing_service.py:330:5
+ --> tests/unit_tests/services/test_model_load_balancing_service.py:400:5
ERROR Object of class `ModelLoadBalancingService` has no attribute `provider_manager` [missing-attribute]
- --> tests/unit_tests/services/test_model_load_balancing_service.py:349:5
+ --> tests/unit_tests/services/test_model_load_balancing_service.py:419:5
ERROR Object of class `ModelLoadBalancingService` has no attribute `provider_manager` [missing-attribute]
- --> tests/unit_tests/services/test_model_load_balancing_service.py:369:5
+ --> tests/unit_tests/services/test_model_load_balancing_service.py:439:5
ERROR Object of class `ModelLoadBalancingService` has no attribute `provider_manager` [missing-attribute]
- --> tests/unit_tests/services/test_model_load_balancing_service.py:390:5
+ --> tests/unit_tests/services/test_model_load_balancing_service.py:460:5
ERROR Object of class `ModelLoadBalancingService` has no attribute `provider_manager` [missing-attribute]
- --> tests/unit_tests/services/test_model_load_balancing_service.py:412:5
+ --> tests/unit_tests/services/test_model_load_balancing_service.py:482:5
ERROR Object of class `ModelLoadBalancingService` has no attribute `provider_manager` [missing-attribute]
- --> tests/unit_tests/services/test_model_load_balancing_service.py:443:5
+ --> tests/unit_tests/services/test_model_load_balancing_service.py:513:5
ERROR Object of class `ModelLoadBalancingService` has no attribute `provider_manager` [missing-attribute]
- --> tests/unit_tests/services/test_model_load_balancing_service.py:465:5
+ --> tests/unit_tests/services/test_model_load_balancing_service.py:541:5
ERROR Object of class `ModelLoadBalancingService` has no attribute `provider_manager` [missing-attribute]
- --> tests/unit_tests/services/test_model_load_balancing_service.py:498:5
+ --> tests/unit_tests/services/test_model_load_balancing_service.py:584:5
ERROR Object of class `ModelLoadBalancingService` has no attribute `provider_manager` [missing-attribute]
- --> tests/unit_tests/services/test_model_load_balancing_service.py:547:5
+ --> tests/unit_tests/services/test_model_load_balancing_service.py:644:5
ERROR Object of class `ModelLoadBalancingService` has no attribute `provider_manager` [missing-attribute]
- --> tests/unit_tests/services/test_model_load_balancing_service.py:578:5
+ --> tests/unit_tests/services/test_model_load_balancing_service.py:675:5
ERROR Object of class `ModelLoadBalancingService` has no attribute `provider_manager` [missing-attribute]
- --> tests/unit_tests/services/test_model_load_balancing_service.py:606:5
+ --> tests/unit_tests/services/test_model_load_balancing_service.py:703:5
ERROR Object of class `ModelLoadBalancingService` has no attribute `provider_manager` [missing-attribute]
- --> tests/unit_tests/services/test_model_load_balancing_service.py:625:5
+ --> tests/unit_tests/services/test_model_load_balancing_service.py:722:5
ERROR Object of class `ModelLoadBalancingService` has no attribute `provider_manager` [missing-attribute]
- --> tests/unit_tests/services/test_model_load_balancing_service.py:647:5
+ --> tests/unit_tests/services/test_model_load_balancing_service.py:744:5
ERROR Object of class `ModelLoadBalancingService` has no attribute `model_assembly` [missing-attribute]
- --> tests/unit_tests/services/test_model_load_balancing_service.py:673:37
+ --> tests/unit_tests/services/test_model_load_balancing_service.py:770:37
ERROR `(tenant_id: str) -> service_with_fake_configurations._FakeProviderManager` is not assignable to attribute `_get_provider_manager` with type `(tenant_id: str) -> ProviderManager` [bad-assignment]
--> tests/unit_tests/services/test_model_provider_service_sanitization.py:74:33
ERROR Argument `dict[str, Any]` is not assignable to parameter `utm_info` with type `UtmInfo` in function `services.operation_service.OperationService.record_utm` [bad-argument-type]
|
Pyrefly Type Coverage
|
|
Hi! I'm I would like to apply some automated changes to this pull request, but it looks like I don't have the necessary permissions to do so. To get this pull request into a mergeable state, please do one of the following two things:
|
Assisted-by: Codex:GPT-5.4
Assisted-by: Codex:GPT-5.4
Assisted-by: Codex:GPT-5.4
|
After a private discussion with @hjlarry, we concluded that a data migration is likely a simpler and more complete solution to this problem. The main considerations are as follows:
Considering the points above, we believe a data migration is the more robust approach. |
AI disclosure: This PR was primarily drafted with Codex using GPT-5.4. I have reviewed and edited the final diff, and I am responsible for the content.
Closes #36426 #36129 #36386
Summary
This fixes a regression in provider-side model credential reads for workspaces that still contain legacy
model_typevalues in provider-related tables.The important change in this revision is that the compatibility logic is now deterministic even when canonical and legacy rows coexist for the same logical scope:
model_typestring and prefer canonical rows during assembly; legacy rows are used only when the canonical counterpart does not exist for that scopeThis keeps new writes on canonical enum values while making mixed datasets readable in a predictable way.
Historical context
There are two layers of
ModelTypehistory that matter here:Definition-level semantics
text-generation,embeddings, andspeech2textdirectly.ModelTypevalues (llm,text-embedding,rerank,speech2text,moderation) plus compatibility helpers such asvalue_of()andto_origin_model_type().tts.Persistence-layer semantics
ModelTypevalues existed in code, provider-related tables could still persist legacy/origin strings for compatibility.model_typecolumns started using canonical enum values instead of legacy strings.text-generation,embeddings, andreranking, while new rows usedllm,text-embedding, andrerank.The short version is:
Cross references
1344c3b280) switched several provider tables toEnumText(ModelType)and removed the olderto_origin_model_type()query normalization from controller/provider read paths.e138523123) restored backward-compatible deserialization inEnumText.process_result_value(), so legacy persisted values could be loaded after a row was already selected.graphonpackage to0.4.0. That upgrade did not introduce a newModelTypesemantic change, but it helped surface the remaining gap in Dify's query compatibility over mixed legacy/canonical rows.graphonrepository itself is not the source of this semantic transition. Its visible releases inherit the already-stable canonical/legacy mapping that Dify had established earlier.Canonical vs legacy mapping
At the moment, the important compatibility pairs are:
llm<->text-generationtext-embedding<->embeddingsrerank<->rerankingThe other currently supported values (
speech2text,moderation,tts) did not undergo the same string transition.Root cause
ModelType.value_of("text-generation")still maps toModelType.LLM, so once a row is fetched the in-memory value is fine.The original failure happened earlier in SQL filtering:
llmmodel_type = 'llm'text-generationwere skipped before deserialization ever ranAfter the first compatibility patch, another issue remained: if both canonical and legacy rows existed for the same logical scope, an
OR-style compatibility filter could still produce ambiguous or unstable results. This PR removes that ambiguity by splitting the handling for single-row and multi-row queries.Query inventory
Single-row queries
provider + model + model_typeProviderConfiguration._get_custom_model_record()scalar_one_or_none()credential_id + model scopeProviderConfiguration._get_specific_custom_model_credential()scalar_one_or_none()model_typeProviderManager.get_default_model()db.session.scalar()ProviderManager.update_default_model_record()db.session.scalar()model + model_typeProviderConfiguration._get_provider_model_setting()first()config_id + model scopeModelLoadBalancingService.get_load_balancing_config()db.session.scalar(...limit(1))ops_trace_manager._lookup_llm_credential_info()session.scalar()Single-row queries inside write flows
ProviderConfiguration.validate_custom_model_credentials()scalar_one_or_none()ProviderConfiguration.update_custom_model_credential()scalar_one_or_none()ProviderConfiguration.delete_custom_model_credential()scalar_one_or_none()ProviderConfiguration.add_model_credential_to_model()scalar_one_or_none()ProviderConfiguration.switch_custom_model_credential()scalar_one_or_none()credential_idwhile updating load-balancing configsModelLoadBalancingService.update_load_balancing_configs()db.session.scalar(...limit(1))config_idduring validationModelLoadBalancingService.validate_load_balancing_credentials()db.session.scalar(...limit(1))Multi-row queries
provider + model + model_typescopeProviderManager.get_provider_model_available_credentials()all()provider + model + model_typescopeModelLoadBalancingService.get_load_balancing_configs()all()ModelLoadBalancingService.update_load_balancing_configs()all()ProviderManager._get_all_provider_models()ProviderManager._get_all_provider_model_settings()ProviderManager._get_all_provider_model_credentials()ProviderManager._get_all_provider_load_balancing_configs()Approach in this PR
1. Single-row queries: canonical-first fallback
For
scalar_one_or_none(),scalar(), andfirst()style lookups, this PR no longer relies on a singlecanonical OR legacyfilter.Instead, the flow is explicit:
This guarantees that when both canonical and legacy rows exist, the canonical row wins deterministically.
2. Multi-row queries: persisted-type-aware preference
For
all()and bulk-iteration query paths, a pure fallback query is not sufficient because multiple rows may legitimately be returned.For these paths, the query now includes an extra raw persisted type column using:
sa.type_coerce(model.model_type, sa.String())The assembly layer then applies a small, isolated compatibility rule:
This matters because once ORM deserialization runs, both
llmandtext-generationbecomeModelType.LLMin Python. The raw persisted string is therefore required to resolve precedence correctly.3. Why the logic is isolated
The compatibility logic is kept in a dedicated helper module so it can be removed cleanly after the legacy rows are migrated away. The business modules only decide which query category they are in:
User-visible failure
This showed up in
CUS-1232on OpenAI-compatible custom model credential flows:Credential with id ... not foundScope of the fix
This PR applies the deterministic compatibility strategy to provider-side read paths, including:
The important boundary is intentional:
Why this shape
I kept the fix focused on read-path compatibility because it addresses the production failure immediately without mixing the hotfix with a broader data cleanup. A later migration can still normalize old rows once operational risk is lower.
Validation
make lintmake type-checkmake test